*robust z variation beta std
*draw random sample according to Bernsteim index data 
*run tercile quatile decile ventile percentile alphas
*multiply with the ratio to get the corresponding beta 
*get std for betas

************************************************************************************************************
**************************************************Table S.7, first panel on lab experiment****************************************
************************************************************************************************************
*first establish the first loop 
use ratios,clear
keep if _n==1 
tempfile boot
save `boot',replace 

use indices_bootstrap,clear 
keep v1 
rename v1 indices 
merge m:1 indices using data_indices
keep if _merge ==3 
drop _merge 
rename (x1 x2 x3 z1 z2 z3) (initial_price1 initial_price2 initial_price3 discount1 discount2 discount3)
gen chosen1=(choice==1)
gen chosen2=(choice==2)
gen chosen3=(choice==3)
gen cs=_n 
reshape long initial_price discount chosen rating,i(cs) j(option_id)

replace initial_price=-initial_price 
replace discount=-discount
rename initial_price price
append using `boot'
egen v1max=max(v1) 
replace v1=v1max if v1==. 
drop if price==. 


*tercile
bys cs: egen sddiscount = sd(discount)
xtile sd3 = sddiscount, nq(3)
clogit chosen price discount rating if  sd3 == 1, group(cs)
gen alpha3=_b[price]
gen beta3=v1*_b[price]

*quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen price discount rating if  sd4 == 1, group(cs)
gen alpha4=_b[price]
gen beta4=v1*_b[price]


*decile
xtile sd10 = sddiscount, nq(10)
clogit chosen price discount rating if  sd10 == 1, group(cs)
gen alpha10=_b[price]
gen beta10=v1*_b[price]


*ventile
xtile sd20 = sddiscount, nq(20)
clogit chosen price discount rating if  sd20 == 1, group(cs)
gen alpha20=_b[price]
gen beta20=v1*_b[price]


*percentile
xtile sd100 = sddiscount, nq(100)
clogit chosen price discount rating if sd100 == 1, group(cs)
gen alpha100=_b[price]
gen beta100=v1*_b[price]

keep alpha* beta* v1 
keep if _n==1 
gen r=1 
tempfile betarobustzvar
save `betarobustzvar',replace 

*repeat for 1000 iterations
forvalues i=2/1000{
display "Draw is `i"
use ratios,clear
keep if _n==`i'
save `boot',replace 

use indices_bootstrap,clear 
keep v`i' 
rename v`i' indices 
merge m:1 indices using data_indices
keep if _merge ==3 
drop _merge 

rename (x1 x2 x3 z1 z2 z3) (initial_price1 initial_price2 initial_price3 discount1 discount2 discount3)
gen chosen1=(choice==1)
gen chosen2=(choice==2)
gen chosen3=(choice==3)
gen cs=_n 
reshape long initial_price discount chosen rating,i(cs) j(option_id)

replace initial_price=-initial_price 
replace discount=-discount
rename initial_price price
append using `boot'
egen v1max=max(v1) 
replace v1=v1max if v1==. 
drop if price==. 


*tercile
bys cs: egen sddiscount = sd(discount)
xtile sd3 = sddiscount, nq(3)
clogit chosen price discount rating if  sd3 == 1, group(cs)
gen alpha3=_b[price]
gen beta3=v1*_b[price]

*quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen price discount rating if  sd4 == 1, group(cs)
gen alpha4=_b[price]
gen beta4=v1*_b[price]


*decile
xtile sd10 = sddiscount, nq(10)
clogit chosen price discount rating if  sd10 == 1, group(cs)
gen alpha10=_b[price]
gen beta10=v1*_b[price]


*ventile
xtile sd20 = sddiscount, nq(20)
clogit chosen price discount rating if  sd20 == 1, group(cs)
gen alpha20=_b[price]
gen beta20=v1*_b[price]


*percentile
xtile sd100 = sddiscount, nq(100)
clogit chosen price discount rating if sd100 == 1, group(cs)
gen alpha100=_b[price]
gen beta100=v1*_b[price]

keep alpha* beta* v1 
keep if _n==1 
gen r=`i' 
append using `betarobustzvar'
save `betarobustzvar',replace 
}
save betarobustzvar,replace 




*get point estimates
use data_long,clear
keep if condition==0
replace initial_price=-initial_price 
replace discount=-discount
bys cs: egen sddiscount = sd(discount)
xtile sd3 = sddiscount, nq(3)
rename initial_price price
gen v1=1.185
clogit chosen price discount rating if  sd3 == 1, group(cs)
gen alpha3=_b[price]
gen beta3=v1*_b[price]

*quartile
xtile sd4 = sddiscount, nq(4)
clogit chosen price discount rating if  sd4 == 1, group(cs)
gen alpha4=_b[price]
gen beta4=v1*_b[price]


*decile
xtile sd10 = sddiscount, nq(10)
clogit chosen price discount rating if  sd10 == 1, group(cs)
gen alpha10=_b[price]
gen beta10=v1*_b[price]


*ventile
xtile sd20 = sddiscount, nq(20)
clogit chosen price discount rating if  sd20 == 1, group(cs)
gen alpha20=_b[price]
gen beta20=v1*_b[price]


*percentile
xtile sd100 = sddiscount, nq(100)
clogit chosen price discount rating if sd100 == 1, group(cs)
gen alpha100=_b[price]
gen beta100=v1*_b[price]

keep alpha* beta* v1 
keep if _n==1 
gen r=. 
save betarobustzvar_point,replace 


*given point estimates and bootstrap estimates, get BCa confidence intervals for beta
use betarobustzvar,clear
append using betarobustzvar_point 
rename (beta3 beta4 beta10 beta20 beta100) (beta_1 beta_2 beta_3 beta_4 beta_5)


forvalues i=1/5{
gen betahatt`i'=beta_`i' if r==. 
egen betahat`i'=sum(betahatt`i') 
drop betahatt`i'
}

drop if r==. 

forvalues i=1/5{
gen low`i'=(beta_`i'<=betahat`i')
egen sumlow`i'=sum(low`i')
gen z0`i'=invnormal(sumlow`i'/1000)
gen p1`i'=normal(z0`i'+z0`i'-invnormal(0.975))
gen p2`i'=normal(z0`i'+z0`i'+invnormal(0.975))
_pctile beta_`i',nq(1000)
local p1p`i'=max(1,round(p1`i'*1000))
local p2p`i'=min(999,round(p2`i'*1000))
gen lower`i'=r(r`p1p`i'')
gen upper`i'=r(r`p2p`i'')
}

keep betahat* lower* upper* 
keep if _n==1
*standard approach results are from Table 2 Column 2 standard logit on costly information choice sets.
*flexible approach results are from this do file: 
rename (betahat1 betahat2 betahat3 betahat4 betahat5 lower1 upper1 lower2 upper2 lower3 upper3 lower4 upper4 lower5 upper5) (Tercile_point Quartile_point Decile_point Ventile_point Percentile_point Tercile_lb Tercile_ub Quartile_lb Quartile_ub Decile_lb Decile_ub Ventile_lb Ventile_ub Percentile_lb Percentile_ub)
label var Tercile_point "Tercile point estimate"
label var Tercile_lb "Tercile lower bound of confidence interval"
label var Tercile_ub "Tercile upper bound of confidence interval"
label var Quartile_point "Quartile point estimate"
label var Quartile_lb "Quartile lower bound of confidence interval"
label var Quartile_ub "Quartile upper bound of confidence interval"
label var Decile_point "Decile point estimate"
label var Decile_lb "Decile lower bound of confidence interval"
label var Decile_ub "Decile upper bound of confidence interval"
label var Ventile_point "Ventile point estimate"
label var Ventile_lb "Ventile lower bound of confidence interval"
label var Ventile_ub "Ventile upper bound of confidence interval"
label var Percentile_point "Percentile point estimate"
label var Percentile_lb "Percentile lower bound of confidence interval"
label var Percentile_ub "Percentile upper bound of confidence interval"

